package sinoservices.demo.service.impl;

import org.springframework.stereotype.Service;
import sinoservices.demo.entity.CustomerInfo;
import sinoservices.demo.mapper.CustomerInfoMapper;
import sinoservices.demo.service.CustomerInfoService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：乔帅阳
 * @date ：Created in 2021/12/11 22:16
 * 此处异常无做处理、数据库除必要字段外，其他辅助字段不加
 * @modified By：
 */
@Service
public class CustomerInfoServiceImpl implements CustomerInfoService {

    @Resource
    private CustomerInfoMapper customerInfoMapper;

    @Override
    public int saveCustomer(CustomerInfo userinfo) {
        return customerInfoMapper.saveCustomer(userinfo);
    }

    @Override
    public int deleteCustomerById(int id) {
        return customerInfoMapper.deleteCustomerById(id);
    }

    @Override
    public int updateCustomer(CustomerInfo userinfo) {
        return customerInfoMapper.updateCustomer(userinfo);
    }

    @Override
    public CustomerInfo getCustomerInfo(int id) {
        return customerInfoMapper.getCustomerInfo(id);
    }

    @Override
    public List<CustomerInfo> selectAllCustomer() {
        return customerInfoMapper.selectAllCustomer();
    }


    /**
     * 获取每十年最小年纪总和
     * @return
     */
    @Override
    public Integer selectMinSum() {

        /**
         * 1、配合数据库，此处创建多次连接效率较低（有连接池的话当前范围可以忍掉），要根据实际需求进一步优化
         * 2、下面给出固定值，如果需要动态的，前端将最大值和最小值传进来之后可实现动态
         */
        int sum = 0;
        int j = 1950;
        for(int i=1950;i<=2020;i+=10){
            j += 10;
           if(i == 2020){
               break;
           }
            int k = customerInfoMapper.queryMinSum(i,j);
            sum += k;
        }
        return sum;
    }

       /*  纯算法，Integer代表比规则里更大的值
        int sum=0,
        int min=Integer.MAX
        for(int i=1950;i<=2020;i+=10){
            for(int j=i;j<=j+10;j++){
                if(2021-j<min) min=2021-j;
            }
            if(min!=Integer.MAX) sum+=min;
        }
        */
}
